//
//  HSLeftViewController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/27.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit
import SnapKit

class HSLeftViewController: UIViewController {
    ///分数分级视图
    @IBOutlet weak var leftScorelBGView: UIView!
    ///头像宽度约束
    @IBOutlet weak var leftIconConstraint: NSLayoutConstraint!
    ///表现分
    @IBOutlet weak var leftScorelLabel: UILabel!
    ///学校名称
    @IBOutlet weak var leftSchoolLabel: UILabel!
    ///班级名称
    @IBOutlet weak var leftClassLabel: UILabel!
    ///账号
    @IBOutlet weak var leftAccLabel: UILabel!
    ///用户名
    @IBOutlet weak var leftNameLabel: UILabel!
    ///头像视图
    @IBOutlet weak var leftIconImageView: UIImageView!
    
    @IBOutlet var leftBtnArray: [UIButton]!
    
    @IBAction func leftBtnClicked(_ sender: UIButton) {
        switch sender.currentTitle! {
        case "课程表":
            present(HSClassTabelViewController(), animated: true, completion: nil)
        case "关于我们":
            let auvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSAboutUsViewController")
            present(auvc, animated: true, completion: nil)
        case "意见反馈":
            let FbVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSFeedbackViewController")
            present(FbVc, animated: true, completion: nil)
        case "退出":
          
            let alertVC = UIAlertController(title: "确定要退出吗?", message: nil, preferredStyle: .alert)
            alertVC.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) in
                let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLoginViewController")
                
                UIApplication.shared.keyWindow?.rootViewController = loginVC
                UserDefaults.standard.set(false, forKey: isLogin)
            }))
            alertVC.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            present(alertVC, animated: true, completion: nil)
            
            

        default:
            break
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        
    }

        ///设置UI
    fileprivate func setUI() {
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        leftIconConstraint.constant = screenW <= 320 ? 60 : 80
        leftIconImageView.layer.cornerRadius = leftIconConstraint.constant/2
        leftIconImageView.layer.masksToBounds = true
        leftIconImageView.layer.borderWidth = 2
        leftIconImageView.layer.borderColor = UIColor.white.cgColor
        
        for btn in leftBtnArray {
            //先获得背景图片
            let image = UIImage.hhq_image(color: UIColor().colorWithHex(0xf0f0f0), size: btn.bounds.size)
            btn.setBackgroundImage(image, for: .highlighted)
        }
        
        let userInfo = NSDictionary(contentsOfFile: saveUserInfoPath)?["data"] as? NSDictionary
      //头像
        if let iconStr = userInfo?["studentphoto"] as? String {
            let iconData = Data(base64Encoded: iconStr, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
                leftIconImageView.image = UIImage(data: iconData!)
        }
        //姓名
        leftNameLabel.text = (userInfo?["studentName"] as? String ?? "") + "    家长"
        //账号
        leftAccLabel.text = userInfo?["studentNum"] as? String
        leftSchoolLabel.text = userInfo?["schoolname"] as? String
        leftScorelLabel.text = userInfo?["studentScore"] as? String

        /*
         address = "*";
         classid = 1071394;
         classname = "\U4e94(1)\U73ed";
         grade = "\U4e94\U5e74\U7ea7";
         id = 6;
         key = 6;
         qq = "*";
         schoolname = "\U9f99\U817e\U4e91\U6559\U80b2\U79d1\U6280";
         sex = "\U7537";
         studentName = "\U674e\U6c9b\U9752";
         studentNum = 5106;
         studentScore = 1;
         studentphoto =
        */
        
        let scorel = Int(leftScorelLabel.text ?? "0") ?? 0
        leftScorelLabel.text = "\(scorel)"
        var level = 0
        switch scorel {
        case 0..<10:
            level = 0
        case 10..<50:
            level = 1
        case 50..<150:
            level = 2
        case 150..<350:
            level = 3
        case 350..<850:
            level = 4
        default:
            if scorel < 0 {
                break
            } else {
                level = 5
            }
        }
        
        //添加分数等级视图
        for i in 1...5 {
            let imageName = i > level ? "jujube\(i)_dark" : "jujube\(i)_light"
            let imageV = UIImageView(image: UIImage(named: imageName))
            imageV.tag = i + 99
            leftScorelBGView.addSubview(imageV)
        }
    }
    
    //视图的生命周期方法
    override func viewDidAppear(_ animated: Bool) {
        //获得背景视图的实际宽度
        let bgViewW = leftScorelBGView.frame.width
        leftScorelBGView.backgroundColor = UIColor().colorWithHex(0x009ee7)
        let imageW = bgViewW/5.0
        
        for iv in leftScorelBGView.subviews {
            
            let x = imageW * CGFloat(iv.tag - 100) + iv.bounds.width/2 + 5
            iv.snp.makeConstraints({ (make) in
            
                make.centerX.equalTo(leftScorelBGView.snp.left).offset(x)
                make.bottom.equalTo(leftScorelBGView.snp.bottom).offset(-5)
                
            })
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }

}
